package com.qfedu.controller;

import com.github.pagehelper.Page;
import com.qfedu.common.JsonResult;
import com.qfedu.entity.Anonymity;
import com.qfedu.entity.User;
import com.qfedu.service.AnonymityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * projectName: backStage
 * author: CGY
 * time: 2020/10/7 9:33
 * description:
 */
@Controller
@RequestMapping("/anonymity")
public class AnonymityController {
    @Autowired
    private AnonymityService anonymityService;

    /**
     * 用户后台 匿名投诉功能
     * @param content
     * @param session
     * @return
     */
    @RequestMapping("add.do")
    @ResponseBody
    public JsonResult add(String content, HttpSession session){
        User user = (User) session.getAttribute("loginUser");

        Anonymity anonymity = new Anonymity();
        anonymity.setContent(content);
        anonymity.setU_no(user.getNo());

        anonymityService.add(anonymity);
        return new JsonResult(1,"添加成功！");
    }

    /**
     * 分页功能展示
     * @param page
     * @param limit
     * @param session
     * @return
     */
    @ResponseBody
    @RequestMapping("queryAnonymity.do")
    public Map<String,Object> queryAnonymity(Integer page,Integer limit,HttpSession session){
        List<Anonymity> list = anonymityService.select(page, limit);

        // 获取总记录数
        long total = ((Page) list).getTotal();

        HashMap<String, Object> map = new HashMap<>();
        map.put("code", 0);
        map.put("msg", "");
        map.put("count", total);
        map.put("data", list);
        System.out.println(map);
        return map;
    }
}
